package com.xu.sms;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.xu.sms.controller.FBOTypeFragment;
import com.xu.sms.controller.HotFragment;
import com.xu.sms.controller.SearchActivity;
import com.xu.sms.utils.LogUtil;


public class MainActivity extends Activity implements OnClickListener {

	private Context context;
	private LinearLayout main_ll_hot;
	private LinearLayout main_ll_festival;
	private LinearLayout main_ll_benison;
	private LinearLayout main_ll_other;
	
	private ImageView main_iv_hot;
	private ImageView main_iv_festival;
	private ImageView main_iv_benison;
	private ImageView main_iv_other;
	private static String TAG = MainActivity.class.getSimpleName();
	
	public  int clickStateId = R.id.main_iv_hot;

	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        initWidget();
        setLinearLayoutClick();
        //设置默认的页面
        setDefaultFragment();
       // 显示和隐藏Actionbar
        // ActionBar bar = getActionBar();
         //bar.setBackgroundDrawable()
       // bar.hide();
    }


    




    //初始化控件
	private void initWidget() {
		//底部导航栏的LinearLayout的实例化
		//String str = "main_ll_hot";
		
		main_ll_hot = (LinearLayout) findViewById(R.id.main_ll_hot);
		main_ll_festival = (LinearLayout) findViewById(R.id.main_ll_festival);
		main_ll_benison = (LinearLayout) findViewById(R.id.main_ll_benison);
		main_ll_other = (LinearLayout) findViewById(R.id.main_ll_other);
		
		//底部导航栏的ImageView的实例化
		main_iv_hot = (ImageView) findViewById(R.id.main_iv_hot);
		main_iv_benison = (ImageView) findViewById(R.id.main_iv_benison);
		main_iv_festival = (ImageView) findViewById(R.id.main_iv_festival);
		main_iv_other = (ImageView) findViewById(R.id.main_iv_other);
	}
	
	//设置控件点击事件
	public void setLinearLayoutClick(){
		main_ll_hot.setOnClickListener(this);
		main_ll_festival.setOnClickListener(this);
		main_ll_benison.setOnClickListener(this);
		main_ll_other.setOnClickListener(this);
	}
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //用户处理按钮被选中的的情况
        switch (item.getItemId()) {
		case R.id.input_search:
			Intent intent = new Intent(context,SearchActivity.class);
			startActivity(intent);
			break;
		}
        return super.onOptionsItemSelected(item);
    }

    //判断你点击那个Fragment
    private HotFragment hotFragment =null ;
    private FBOTypeFragment otherFragment = null ;
    private FBOTypeFragment festivalFragment = null;
    private FBOTypeFragment bensionFragment = null ;
    
    
    private void setDefaultFragment(){
    	FragmentManager manager = getFragmentManager();
		FragmentTransaction tr = manager.beginTransaction();
		if(hotFragment ==null){
			hotFragment = new HotFragment();
		}
    	tr.replace(R.id.main_ll,hotFragment );
		main_iv_hot.setImageResource(R.drawable.ic_hot_checked);
    	tr.commit();
    }
    
    
    private int llclickstate = R.id.main_ll_hot;
	@Override
	public void onClick(View v) {
		//不做重复点击的事情
		if(v.getId() == llclickstate){
			return;
		}
		FragmentManager manager = getFragmentManager();
		FragmentTransaction tr = manager.beginTransaction();
		switch (v.getId()) {
		case R.id.main_ll_hot:
			LogUtil.i(TAG,120, "main_ll_hot");
			setShowFragment(R.id.main_ll_hot,tr);
			llclickstate = R.id.main_ll_hot;
			break;
		case R.id.main_ll_festival:
			LogUtil.i(TAG, 125,"main_ll_festival");
			setShowFragment(R.id.main_ll_festival,tr);
			llclickstate = R.id.main_ll_festival;
			break;
		case R.id.main_ll_benison:
			LogUtil.i(TAG, 130,"main_ll_benison");
			setShowFragment(R.id.main_ll_benison,tr);
			llclickstate = R.id.main_ll_benison;
			break;
		case R.id.main_ll_other:
			LogUtil.i(TAG,135, "main_ll_other");
			setShowFragment(R.id.main_ll_other,tr);
			llclickstate = R.id.main_ll_other;
			break;
		}
		
		tr.commit();
	}
	
	//设置要显示的fragment和
	public void setShowFragment(int id,FragmentTransaction tr){
		switch (id) {
		case R.id.main_ll_hot:
			if(hotFragment ==null){
				hotFragment = new HotFragment();
			}
			tr.replace(R.id.main_ll,hotFragment );
			main_iv_hot.setImageResource(R.drawable.ic_hot_checked);
			closeOther(R.id.main_iv_hot,clickStateId);
			break;
		case R.id.main_ll_festival:
			if(festivalFragment == null){
				festivalFragment = new FBOTypeFragment(FBOTypeFragment.TYPE_PARENT_FESTIVAL_ID);
			}
			tr.replace(R.id.main_ll, festivalFragment);
			main_iv_festival.setImageResource(R.drawable.ic_festival_checked);
			closeOther(R.id.main_iv_festival,clickStateId);
			break;
		case R.id.main_ll_benison:
			if(bensionFragment == null){
				bensionFragment = new FBOTypeFragment(FBOTypeFragment.TYPE_PARENT_BENTSION_ID);
			}
			tr.replace(R.id.main_ll, bensionFragment);
			main_iv_benison.setImageResource(R.drawable.ic_benison_checked);
			closeOther(R.id.main_iv_benison,clickStateId);
			break;
		case R.id.main_ll_other:
			if(otherFragment == null){
				otherFragment = new FBOTypeFragment(FBOTypeFragment.TYPE_PARENT_OTHER_ID);
			}
			tr.replace(R.id.main_ll,otherFragment);
			main_iv_other.setImageResource(R.drawable.ic_other_checked);
			closeOther(R.id.main_iv_other,clickStateId);
			break;
		}
	}
	
	//激活当前关闭其他
	private void closeOther(int activeId,int closeiId){
		switch (closeiId) {
		case R.id.main_iv_hot:
			main_iv_hot.setImageResource(R.drawable.ic_hot_normal);
			break;
		case R.id.main_iv_benison:
			main_iv_benison.setImageResource(R.drawable.ic_benison_normal);
			break;
		case R.id.main_iv_festival:
			main_iv_festival.setImageResource(R.drawable.ic_festival_normal);
			break;
		case R.id.main_iv_other:
			main_iv_other.setImageResource(R.drawable.ic_other_normal);
			break;
		}
		clickStateId = activeId;
	} 
}
